import axios from 'axios';
import router from '../router';
import store from '../utils/store';


axios.defaults.withCredentials = true;

axios.interceptors.request.use(
    config => {
        const token = store.get('token');
        // console.log(token);
        config.headers.Accept = '*/*';
        // 请求头
        if (token) {
            config.headers.Authorization = `Bearer ${token}`;
        }
        return config;
    },
    error => {
        return Promise.reject(error);
    }
);

axios.interceptors.response.use(
    response => {
        if (response.data.code && response.data.code !== 200) {

        }
        return response.data;
    },
    error => {
        // console.log(error.response);
        if (error.response.data.code === 520) {
            router.replace('/login');
        }
        return Promise.reject(error);
    }
);
